#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define HIGH 10
#define WIDE 10
#define NUM 10


//调试打印
void test2(int chessboard[3][HIGH][WIDE])
{
	int a, b, c;
	for (a = 0; a < 3; a++)
	{
		for (b = 0; b < HIGH; b++)
		{
			for (c = 0; c < WIDE; c++)
			{
				printf("%2d", chessboard[a][b][c]);
			}
			printf("\n");
		}
		printf("\n\n");
	}
}
void test3(int chessboard[3][HIGH][WIDE])
{

	int i, j;
	for (i = 0; i < HIGH; i++)
	{
		for (j = 0; j < WIDE; j++)
		{
			if (chessboard[0][i][j] == 0) printf("  ");
			else if (chessboard[0][i][j] == -1) printf(" *");
			else printf("%2d", chessboard[0][i][j]);
		}
		printf("\n");
	}
	printf("\n\n");
}


//初始化扫雷程序
void setboard(int chessboard[3][HIGH][WIDE])
{
	int i,kx,ky,ktx,kty,seed,a,b,c;
	for (a=0;a<3;a++)
	{
		for (b=0;b<HIGH;b++)
		{
			for (c = 0; c < WIDE; c++)
			{
				chessboard[a][b][c] = 0;
			}
		}
	}
	seed = time(NULL);
	srand(seed);
	for (i = 0; i < NUM; )
	{
		kx = rand(); kx %= WIDE;
		ky = rand(); ky %= HIGH;
		if (chessboard[0][ky][kx] == -1);
		else 
		{
			chessboard[0][ky][kx] = -1;
			i++;
			ktx = kx + 1; kty = ky; if (ktx < WIDE && ktx >=0 && kty < HIGH && kty >=0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
			ktx = kx + 1; kty = ky + 1; if (ktx < WIDE && ktx >= 0 && kty < HIGH && kty >= 0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
			ktx = kx + 1; kty = ky - 1; if (ktx < WIDE && ktx >= 0 && kty < HIGH && kty >= 0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
			ktx = kx - 1; kty = ky; if (ktx < WIDE && ktx >= 0 && kty < HIGH && kty >= 0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
			ktx = kx - 1; kty = ky + 1; if (ktx < WIDE && ktx >= 0 && kty < HIGH && kty >= 0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
			ktx = kx - 1; kty = ky - 1; if (ktx < WIDE && ktx >= 0 && kty < HIGH && kty >= 0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
			ktx = kx; kty = ky + 1; if (ktx < WIDE && ktx >= 0 && kty < HIGH && kty >= 0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
			ktx = kx; kty = ky - 1; if (ktx < WIDE && ktx >= 0 && kty < HIGH && kty >= 0 && chessboard[0][kty][ktx] != -1) chessboard[0][kty][ktx] += 1;
		}
	}
}

//帮助
void Help()
{
	printf("1.胜利条件：你需要在不点错雷的情况下将所有的雷都标记出来，如果你点错，就得重新开始\n");
	printf("2.如何操作：选中一个格子，并选择翻开、标记为雷或者取消标记");
	printf("3.操作格式：坐标+操作选项，操作选项分别为：\n      1 翻开\n      2 标记/取消标记\n例如  1 2 1\n表示为：翻开第一排的第二个的格子\n");
}

//关于
void About()
{
	printf("制作：seewhy\n制作时间：2022.10.27\n感谢游玩\n");
}

//打印雷盘/0、1、2分别是真实区、掩埋区和标记区/-1>>雷、0>>空、1>>挖开
void prboard(int chessboard[3][HIGH][WIDE])
{
	int i, j;
	char x[10][3]={ "一","二","三","四","五","六","七","八","九","十"};
	printf("  一二三四五六七八九十\n");
	for (i = 0; i < HIGH; i++)
	{
		printf("%s", x[i]);
		for (j = 0; j < WIDE; j++)
		{
			if (chessboard[2][i][j] == -1) printf("田");
			else if (chessboard[1][i][j] == 0) printf("口");
			else if (chessboard[0][i][j] == 0) printf("  ");
			else if (chessboard[0][i][j] == -1) printf(" *");
			else printf("%2d", chessboard[0][i][j]);
		}
		printf("%s\n",x[i]);
	}
	printf("  一二三四五六七八九十\n");
}

//自动翻格子递归函数
void test(int chessboard[3][HIGH][WIDE], int x, int y)
{
	if (chessboard[1][y][x] == 0 && chessboard[2][y][x] == 0&&x>=0&&x<WIDE&&y>=0&&y<HIGH)
	{
		if (chessboard[0][y][x] == 0)
		{
			chessboard[1][y][x] = 1;
			test(chessboard, x, y + 1);
			test(chessboard, x, y - 1);
			test(chessboard, x + 1, y);
			test(chessboard, x - 1, y);
		}
		else chessboard[1][y][x] = 1;
	}
}

//翻开格子
int dig(int chessboard[3][HIGH][WIDE], int x, int y)
{
	//若选择翻开的格子已经被标记>>继续游戏
	if (chessboard[2][y][x] == -1)
	{
		printf("该处已被标记");
		return 1;
	}
	//若...是雷>>结束游戏
	if (chessboard[0][y][x] == -1)
	{
		chessboard[1][y][x] = 1;
		prboard(chessboard);
		printf("踩雷啦！\n\n");
		return 0;
	}
	//若...已经被翻开>>继续游戏
	if (chessboard[1][y][x] == 1) return 1;
	//若翻开的数字不是0>>继续游戏
	if (chessboard[0][y][x] != 0)
	{
		chessboard[1][y][x] = 1;
		return 1;
	}
	//自动开始翻格子
	test(chessboard, x, y);
	return 1;
}

//标雷
int flag(int chessboard[3][HIGH][WIDE], int x, int y, int *k)
{
	if (chessboard[1][y][x] == 1)
	{
		printf("标记失败，该处已挖开\n");
		return 1;
	}
	else
	{
		if (chessboard[2][y][x] == -1)
		{
			chessboard[2][y][x] = 0;
			if (chessboard[0][y][x] == -1)
				(* k)--;
			printf("已取消该点标记\n");
			return 1;
		}
		else
		{
			chessboard[2][y][x] = -1;
			printf("标记成功！\n");
			printf("%d\n", *k);
			if (chessboard[0][y][x] == -1)
				(* k)++;
			printf("%d\n", *k);
			if (* k == NUM)
			{
				prboard(chessboard);
				printf("胜利！\n");
				return 0;
			}
			return 1;
		}
	}
}